package com.douziit.meilianoa.base.generator;



import com.douziit.meilianoa.base.enums.EncType;
import com.douziit.meilianoa.base.utils.StrEncryptUtil;
import com.douziit.meilianoa.base.utils.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * 重新spring配置文件加载
 *
 * @author MrShun
 * @version 1.0
 * @Date 2017-11-17
 */
public class PropertyGenerateDefine extends PropertyPlaceholderConfigurer {

    private static final Logger LOG = LoggerFactory.getLogger(PropertyGenerateDefine.class);

    /**
     * 开发环境：dev开发环境；tes测试环境；pro线上环境
     */
    private String model;

    /**
     * //定义模式字符串 //定义加密符号 //定义密码
     */
    private String envModeStr = "env.mode", encSymbol = "@", password = "owl_lzw_66666666";

    /**
     * 定义三种模式
     */
    private String[] modes = {"dev", "tes", "pro"};

    /**
     * 解密属性配置并且, 灵活切换配置
     * 可以动态加载网络上面的配置
     *
     * @param props
     */
    @Override
    protected void convertProperties(Properties props) {
        this.model = props.getProperty(envModeStr);
        if (StrUtil.isBlank(model)) {
            super.convertProperties(props);
        } else if (!modes[0].equals(model) && !modes[1].equals(model) && !modes[2].equals(model)) {
            throw new RuntimeException("env.mode 配置只能是 dev/tes/pro 三种模式");
        } else {
            Set<Map.Entry<Object, Object>> entries = props.entrySet();
            Properties newProperties = new Properties();
            for (Map.Entry<Object, Object> entry : entries) {
                String key = String.valueOf(entry.getKey());
                String value = String.valueOf(entry.getValue());
                if (key.startsWith(model)) {
                    key = key.substring(4, key.length());
                    newProperties.setProperty(key, value);
                } else if (key.startsWith(encSymbol.concat(model).concat("."))) {
                    key = key.substring(5, key.length());
                    value = StrEncryptUtil.decrypt(password, EncType.AES, value);
                    newProperties.setProperty(key, value);
                } else if (key.startsWith(encSymbol) && key.indexOf("dev.") != -1 && key.indexOf("test.") != -1 && key.indexOf("pro.") != -1) {
                    key = key.substring(1, key.length());
                    value = StrEncryptUtil.decrypt(password, EncType.AES, value);
                    newProperties.setProperty(key, value);
                } else if (key.indexOf("dev.") != -1 || key.indexOf("test.") != -1 || key.indexOf("pro.") != -1) {
                    continue;
                }

            }
            props.putAll(newProperties);
            super.convertProperties(newProperties);
        }
    }


    @Override
    public String convertProperty(String propertyName, String propertyValue) {
        LOG.debug("key = {} -> value = {}", propertyName, propertyValue);
        return super.convertProperty(propertyName, propertyValue);
    }


}
